<?php
            
use app\model\Record; 
use app\model\Theme; 
use app\model\Fans; 


/**
 * Desc:
 * Time: 2021/5/27 4:08 下午
 */
class Index_Page extends PluginMobilePage {

    function main()
    {
        global $_P,$_G,$_S,$_C;
        $fromtype = $_P['from'];
        if ($fromtype=='mobile') {
            return; //退出程序执行
        } elseif ($fromtype=='wxapp') {
            $themeid = isset($_P['themeid'])?$_P['themeid']:'';
            $fromuid = isset($_P['fromuid'])?$_P['fromuid']:'';
            $workerid = isset($_P['workerid'])?$_P['workerid']:'';
            $themeid = isset($_P['themeid'])?$_P['themeid']:'';
            $theme = Theme::getThemeById($themeid);

            if (!$themeid || !$theme['id']) {
                return_json(1,'活动入口不正确');
            } else {
                //基本检测
                $fans = $this->checkuser();
                // var_dump($fans);
                // $fromuid = 11500;
                // $fans = Fans::getById(11500,$themeid);

                //判断用户是否存在订单
                $themyrecord = Record::getLastGroupByUid($fans['uid'],$themeid);
                $res = LingzeModel::initCheck($theme,$_C,$fans,$fromuid,$themyrecord);
                $format_res = $res;
                return_json(0,'',$format_res);
            }
        }
    }
    //立即参与
    function joinActivity()
    {
        global $_P,$_G,$_S,$_C;
        $fromtype = $_P['from'];
        if ($fromtype=='mobile') {
            return; //退出程序执行
        } elseif ($fromtype=='wxapp') {
            $themeid = isset($_P['themeid'])?$_P['themeid']:'';
            $fromuid = isset($_P['fromuid'])?$_P['fromuid']:'';
            $workerid = isset($_P['workerid'])?$_P['workerid']:'';
            $themeid = isset($_P['themeid'])?$_P['themeid']:'';
            $theme = Theme::getThemeById($themeid);

            if (!$themeid || !$theme['id']) {
                return_json(1,'活动入口不正确');
            } else {
                //基本检测
                $fans = $this->checkuser();
                //判断用户是否存在实例
                $has_entry = LingzeModel::getUserEntry($fans['uid'],$themeid);
                if (!$has_entry){
                    $insert_data = [
                        'uid'=>$fans['uid'],
                        'lz_chest_sort'=>0,
                        'share_num'=>0,
                        'friend_view_num'=>0,
                        'friend_order_num'=>0,
                        'themeid'=>$themeid
                    ];
                    $res = LingzeModel::insertEntry($insert_data);
                }
                $format_res = [
                    'error_txt'=>'',
                ];
                return_json(0,'',$format_res);
            }
        }
    }
}